﻿using ICS_ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ICS_Equipment;
using ICS_Equipment.CoolingPump;
using ICS_Equipment.Components;
using ICS_Equipment.platform;

namespace Equipment
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new CoolingPumpViewModel();
        }

        private void BlueCoolingPump_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
            Border border = (Border)sender;
            string id = border.Name;
            DragDrop.DoDragDrop((DependencyObject)sender,id, DragDropEffects.Copy);
            Mouse.Capture(null);
        }
        FrameworkElement instance;
      
        private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //Canvas canvas = sender as Canvas;
            //Point point = e.GetPosition(canvas);
            //canvas.Children.Add(instance);
            //instance.SetValue(Canvas.LeftProperty, point.X);
            //instance.SetValue(Canvas.LeftProperty, point.Y);
        }
        bool _isMove = false;
        FrameworkElement _moveObj = null;
        Point _downPoint = new Point();
        private void BlueCoolingPump_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            _moveObj = sender as ICS_Equipment.CoolingPump.BlueCoolingPump;
            if (_moveObj==null)
            {
                _moveObj = sender as ICS_Equipment.Components.CoolingTower;
            }
            if (_moveObj == null)
            {
                _moveObj = sender as ICS_Equipment.Components.Pipeline;
            }
            if (_moveObj == null)
            {
                _moveObj = sender as ICS_Equipment.platform.platform;
            }
            
            _downPoint = e.GetPosition(_moveObj);
            _isMove = true;
        }

        private void BlueCoolingPump_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isMove)
            {
                Point point = e.GetPosition(this.canvas);
                _moveObj.SetValue(Canvas.LeftProperty,point.X-_downPoint.X );
                _moveObj.SetValue(Canvas.TopProperty,point.Y-_downPoint.Y);
            }
        }

        private void BlueCoolingPump_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _isMove = false;
            _moveObj = null;
        }

        private void BlueCoolingPump_Drop(object sender, DragEventArgs e)
        {

        }

        private void canvas_Drop(object sender, DragEventArgs e)
        {
            string componentName = (string)e.Data.GetData(typeof(string));
            Canvas canvas = sender as Canvas;
            //if (componentName== "platform")
            //{
            //    Viewbox viewbox = new Viewbox();
            //    Polygon polygon = new Polygon();
            //    polygon.VerticalAlignment = VerticalAlignment.Bottom;
            //    polygon.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#AAF7F9FA"));
              
            //    Point point1 ,point2,point3,point4= new Point();
            //    point1.X = 0;
            //    point1.Y = 200;
            //    point2.X = 150;
            //    point2.Y = 0;
            //    point3.X = 1000;
            //    point3.Y = 0;
            //    point4.X = 1200;
            //    point4.Y = 200;
            //    polygon.Points.Add(point1);
            //    polygon.Points.Add(point2);
            //    polygon.Points.Add(point3);
            //    polygon.Points.Add(point4);
            //    polygon.Stroke = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DDDFFDFF"));
            //    polygon.StrokeThickness = 10;

            //    canvas.Children.Add(viewbox);
               
            //    canvas.Children.Add(polygon);

            //}
            //反射拿到组件对象
            Assembly assembly = Assembly.Load("ICS_Equipment");
            Type type = assembly.GetType("ICS_Equipment.CoolingPump." + componentName);
            if (type==null)
            {
                type = assembly.GetType("ICS_Equipment.Components." + componentName);
            }
            if (type == null)
            {
                type = assembly.GetType("ICS_Equipment.platform." + componentName);
            }
          
            instance = (FrameworkElement)Activator.CreateInstance(type);
            instance.Width = 160;
            
            instance.MouseLeftButtonDown += BlueCoolingPump_MouseLeftButtonDown_1;
            instance.MouseMove += BlueCoolingPump_MouseMove;
            instance.MouseLeftButtonUp += BlueCoolingPump_MouseLeftButtonUp;
        
            Point point = e.GetPosition(canvas);
            canvas.Children.Add(instance);
            instance.SetValue(Canvas.LeftProperty,point.X);
            instance.SetValue(Canvas.TopProperty,point.Y);
        }

        private void ItemsControl_Drop(object sender, DragEventArgs e)
        {

        }

        private void platform_MouseWheel(object sender, MouseWheelEventArgs e)
        {

        }
    }
}